// Problem: C - Central Post Office
// Idea: The answer is twice the total number of edges in the tree minus
//       its diameter, which has to be computed in O(n).
// Language: C++
// Author: Ivan Vladimirov Ivanov (ivan.vladimirov.ivanov@gmail.com)

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define MAX 10005

void input(void);
void solve(void);

pair <int, int> dfs(int x, int p, int d);

int n;
vector <int> adj[MAX];

int main(void)
{
  int t;
  scanf("%d", &t);
  while(t--) {
    input();
    solve();
  }

  return 0;
}

void input(void)
{
  scanf("%d", &n);
  for(int i = 1; i <= n; i++) {
    int cnt;
    scanf("%d", &cnt);
    adj[i].clear();
    for(int j = 0; j < cnt; j++) {
      int x;
      scanf("%d", &x);
      adj[i].push_back(x);
    }
  }
}

void solve(void)
{
  pair <int, int> res = dfs(1, -1, 0);
  res = dfs(res.first, -1, 0);
  printf("%d\n", 2 * (n - 1) - res.second);
}

pair <int, int> dfs(int x, int p, int d)
{
  pair <int, int> res(x, d);

  for(vector <int>::iterator it = adj[x].begin(); it != adj[x].end(); it++) {
    if(*it == p) continue;
    pair <int, int> t = dfs(*it, x, d + 1);
    if(t.second > res.second) res = t;
  }

  return res;
}

